package com.h5lib.xlsx4j;

import java.io.Closeable;
import java.io.IOException;

import org.xmlpull.v1.XmlSerializer;

public class XMLSharedStringWriter implements Closeable{

    private XmlSerializer outStream;
    private int index = 0;
    
    
    public XMLSharedStringWriter(XmlSerializer newSerializer) {
    	outStream = newSerializer;
    	writeXmlStart();
    }

    
    private void writeXmlStart() {
    	try {
			outStream.startDocument("UTF-8", true);
			outStream.startTag(null, "sst");
			outStream.attribute(null, "xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    public void addSharedString(String text){
    	try {
			outStream.startTag(null, "si");
			outStream.startTag(null, "t");
			outStream.text(text==null?"":text);
			outStream.endTag(null, "t");
			outStream.endTag(null, "si");
		} catch (Exception e) {
			e.printStackTrace();
		}
    	index++ ;
    }
    
    private void writeXmlEnd() {
    	try {
			outStream.endTag(null, "sst");
			outStream.endDocument();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
	@Override
	public void close() throws IOException {
		writeXmlEnd();
	}


	public int getIndex() {
		return index;
	}


	public void setIndex(int index) {
		this.index = index;
	}
}
